package com.hospital.yang.fchua.service.impl;

import com.github.pagehelper.PageHelper;
import com.hospital.yang.fchua.dao.DoctorMedicineMapper;
import com.hospital.yang.fchua.entity.DoctorMedicine;
import com.hospital.yang.fchua.service.DoctorMedicineService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @description：
 * @author：xyj
 * @date：2021/9/22 19:54
 * @versions：v1.0
 */
@Service
public class DoctorMedicineServiceImpl implements DoctorMedicineService {
    @Autowired
    private DoctorMedicineMapper doctorMedicineMapper;

    /**
     * 药订单分页查询
     *
     * @param name
     * @param pageNo
     * @param pageSize
     * @return
     */
    @Override
    public List<DoctorMedicine> MedicineList(String name, Integer pageNo, Integer pageSize) {
        PageHelper.startPage(pageNo, pageSize);
        return doctorMedicineMapper.MedicineList(name);
    }

    /**
     * 查询药单详情
     *
     * @param userid
     * @param time
     * @return
     */
    @Override
    public List<DoctorMedicine> listdate(Integer userid, String time) {
        Map<String, Object> map = new HashMap<>();
        map.put("userid", userid);
        map.put("time", time);
        return doctorMedicineMapper.listdate(map);
    }

    /**
     * 得到药单的总价格
     *
     * @param list 药单
     * @return 当前药单的总价格
     */
    @Override
    public double money(List<DoctorMedicine> list) {
        //总价格
        double money = 0;
        //循环计算价格
        for (int i = 0; i < list.size(); i++) {
            //计算总价格
            money += list.get(i).getDrugRemedy().getPrice() * list.get(i).getSum();
        }
        return money;
    }

    /**
     * 得到药品的集合
     *
     * @param list 药单
     * @return 药名+数量
     */
    @Override
    public String drugs(List<DoctorMedicine> list) {
        //药品拼接
        String drugs = "";
        //循环拼接药品
        for (int i = 0; i < list.size(); i++) {
            //药品拼接
            drugs += list.get(i).getDrugRemedy().getDrugname() + "*" + list.get(i).getSum();
            if (i < list.size() - 1) {
                drugs += "|";
            }
        }
        return drugs;
    }

    /**
     * 修改药单状态
     *
     * @param list 药单
     */
    @Override
    public void update(List<DoctorMedicine> list) {
        for (int i = 0; i < list.size(); i++) {
            updates(list.get(i).getId());
        }
    }

    /**
     * 用户余额查询
     *
     * @param id
     * @return
     */
    @Override
    public double selectUserMoney(Integer id) {
        return doctorMedicineMapper.selectUserMoney(id);
    }

    /**
     * 更改药单状态
     *
     * @param id 药单id
     * @return 1 修改成功
     */
    @Override
    public Integer updates(int id) {
        return doctorMedicineMapper.updateById(new DoctorMedicine(id, 1));
    }

    /**
     * 分页查询未缴费的药单
     *
     * @param name
     * @param pageNo
     * @param pageSize
     * @return
     */
    @Override
    public List<DoctorMedicine> yaoSelect(String name, Integer pageNo, Integer pageSize) {
        PageHelper.startPage(pageNo, pageSize);
        return doctorMedicineMapper.yaoSelect(name);
    }

    /**
     * 判断用户余额是否能支付药费
     *
     * @param userid    用户编号
     * @param drugMoney 药费
     * @return true 余额充足
     */
    @Override
    public boolean pdMoney(Integer userid, double drugMoney) {
        double doctorMed = doctorMedicineMapper.selectUserMoney(userid);
        if (doctorMed >= drugMoney) {
            Map<String, Object> map = new HashMap<>();
            map.put("money", drugMoney);
            map.put("id", userid);
            int i = doctorMedicineMapper.updateUserMoney(map);
            return true;
        }
        return false;
    }

    /**
     * 修改开药单状态
     *
     * @param userId
     * @return
     */
    @Override
    public boolean update(Integer userId) {
        int i = doctorMedicineMapper.updateById(new DoctorMedicine(userId, 1));
        if (i == 1) {
            return true;
        }
        return false;
    }

    /**
     * 修改余额
     *
     * @param money
     * @param id
     * @return
     */
    @Override
    public boolean updateUserMoney(double money, Integer id) {
        Map<String, Object> map = new HashMap<>();
        map.put("money", money);
        map.put("id", id);
        int i = doctorMedicineMapper.updateUserMoney(map);
        if (i == 1) {
            return true;
        }
        return false;
    }

}
